layui.use(['form', 'layedit', 'laydate','upload','layer'], function(){
    var form = layui.form
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate
        ,upload = layui.upload;



    //自定义验证规则
    form.verify({
        title: function(value){
            if(value.length < 5){
                return '标题至少得5个字符啊';
            }
        }
        ,pass: [
            /^[\S]{6,12}$/
            ,'密码必须6到12位，且不能出现空格'
        ]
        ,content: function(value){
            layedit.sync(editIndex);
        }
    });

    //监听提交
    form.on('submit(demo1)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '最终的提交信息'
        })
        return false;
    });

    $=layui.$;

    $('#username').bind('input propertychange', function() {
        //var username = $(this).value();
        var username = $(this).val()
        $.ajax({
            type:"post",
            url:"/Anime/user/verifyUsername",
            dataType:"json",
            data:{
                'username':username
            },
            success:function (res) {
                if(res.code == 0){
                    $("#usernameVarify").text("")
                }else{
                    $("#usernameVarify").text(res.msg)
                }
            }
        })
    });

    $("#codeBtn").on("click",function (data) {
        var phone = $("#iphone").val()
        if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))){   //^1 以数字1开头 (3|4|5|6|7|8|9)第二位数 \d(9) 任意9位数 $以数字结尾
            layer.msg("手机号码有误，请重填");
        }else{
            $.ajax({
                type:"post",
                url:"/Anime/home/mobileCode",
                dataType:"json",
                data:{
                    "phone":phone
                },
                success:function (res) {
                    if(res.code == 0){
                        layer.msg(res.msg)
                    }else{
                        layer.msg(res.msg)
                    }
                }
            })
        }
    })

    $('#iphone').bind('input propertychange',function () {
        var phone = $("#iphone").val()
        if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))){   //^1 以数字1开头 (3|4|5|6|7|8|9)第二位数 \d(9) 任意9位数 $以数字结尾
            $("#phoneVarify").text("请输入有效的手机号")
        }else{
            $("#phoneVarify").text("")
        }
    })

    $('#password').bind('input propertychange', function () {
        var pwd = $(this).val();
        if(pwd.length<6 || pwd.length>12){
            $("#passwordVarify").text("密码应为6-12位数")
        }else{
            $("#passwordVarify").text("")
        }
    })
    
    //表单取值
    layui.$('#LAY-component-form-getval').on('click', function(){
        var data = form.val('example');
        alert(JSON.stringify(data));
    });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#upImg'
        ,url: '/Anime/home/upImg' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#displayImg').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code == 0){
                layer.msg("上传成功！")
                $("#faceImage").val(res.msg)
            }
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //监听提交
    form.on('submit(register)', function(data){
        var mobileCode = data.field.mobileCode
        if($("#agree").is(":checked") === true){
            $.ajax({
                type:'post',
                url:'/Anime/user/codeVarify',
                dataType:'json',
                data:{
                    "mobileCode":mobileCode
                },
                success:function (res) {
                    if(res.code == 0){
                        $.ajax({
                            type:'post',
                            url:'/Anime/user/register',
                            dataType:'json',
                            data:data.field,
                            success:function (res) {
                                if(res.code == 0){
                                    window.location.href = "/Anime/home/loginPage"
                                }else{
                                    layer.msg(res.msg)
                                }
                            }
                        })
                    }else{
                        layer.msg(res.msg)
                    }
                }
            })
        }else {
            layer.msg("请先阅读《CiLiCiLi动漫论坛协议》！")
        }
        return false;
    });

});

